package com.ilop.sthome.ui.activity.scene;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.mvp.contract.scene.SceneDetailContract;
import com.ilop.sthome.mvp.present.scene.SceneDetailPresenter;
import com.ilop.sthome.common.command.SiterCommand;
import com.ilop.sthome.ui.adapter.scene.SceneDetailAdapter;
import com.ilop.sthome.ui.dialog.TipDialog;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySceneDetailBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-02.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：场景详情页面/自动化列表页面
 */
public class SceneDetailActivity extends ContainLoginActivity<SceneDetailPresenter, ActivitySceneDetailBinding> implements SceneDetailContract.IView {

    private int mSceneId;
    private String mDeviceName;
    private SceneDetailAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_detail;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mSceneId = getIntent().getIntExtra(CommonId.KEY_SCENE_ID, 0);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SceneDetailPresenter(mContext, mDeviceName, mSceneId);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new SceneDetailAdapter(mContext);
        mAdapter.setSceneId(mSceneId);
        mDBind.sceneRecycle.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        LiveDataBus.get().with(CommonId.AUTO_ITEM_CLICK, AutomationBean.class).observe(this, automation -> {
            if (automation!=null) {
                mPresent.skipActivityToUpdate(automation);
            }
        });

        LiveDataBus.get().with(CommonId.AUTO_ITEM_LONG_CLICK, AutomationBean.class).observe(this, automation -> {
            if (automation!=null) {
                TipDialog dialog = new TipDialog(mContext, ()->{
                    showProgressDialog();
                    mPresent.onDeleteAutomation(automation.getMid());
                });
                dialog.setMsg(getString(R.string.delete_automation));
                dialog.show();
            }
        });

        LiveDataBus.get().with(CommonId.CHANGE_SCENE_AUTO, AutomationBean.class).observe(this, automation -> {
            if (automation!=null) {
                showProgressDialog();
                mPresent.onUpdateAuto(automation);
            }
        });

        LiveDataBus.get().with(CommonId.AUTO_CLICK_TO, AutomationBean.class).observe(this, automation -> mPresent.onClickExecution(automation.getMid()));

        LiveDataBus.get().with(CommonId.DELETE_SCENE_SUCCESS, Integer.class).observe(this, integer -> finish());
    }

    @Override
    protected void initListener() {
        super.initListener();

        mDBind.ivSceneBack.setOnClickListener(view -> finish());

        mDBind.sceneAddAuto.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(mContext, ChooseActionActivity.class);
            intent.putExtra(CommonId.KEY_ADD_SCENE, true);
            intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
            startActivity(intent);
        });

        mDBind.addAuto.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(mContext, ChooseActionActivity.class);
            intent.putExtra(CommonId.KEY_ADD_SCENE, true);
            intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
            startActivity(intent);
        });

        mDBind.tvSceneSetting.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, SceneEditActivity.class);
            intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
            intent.putExtra(CommonId.KEY_SCENE_ID, mSceneId);
            startActivity(intent);
        });

        mDBind.sceneRecycle.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresent.getAutoList();
                mDBind.sceneRecycle.complete();
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "initDataBus: answerOK");
                dismissProgressDialog();
                if (event.getData_str1().length() == 9) {
                    String code = event.getData_str2().substring(0, 2);
                    int cmd = Integer.parseInt(event.getData_str1().substring(0, 4), 16);
                    if (cmd == SiterCommand.INCREASE_AUTOMATION) {
                        if (CommonId.KEY_SUCCESS.equals(code)) {
                            mPresent.onUpdateSuccess();
                        } else {
                            showToast(getString(R.string.EE_AS_SEND_EMAIL_FOR_CODE4));
                        }
                    }else if (cmd == SiterCommand.DELETE_AUTOMATION) {
                        if (CommonId.KEY_SUCCESS.equals(code.substring(0, 2))) {
                            showToast(getString(R.string.delete_success));
                            mPresent.deleteSuccess();
                        } else {
                            showToast(getString(R.string.failed));
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.getSceneName();
        mPresent.getAutoList();
    }

    @Override
    public void showSceneName(String name) {
        mDBind.sceneModeName.setText(name);
    }

    @Override
    public void showAutoList(List<AutomationBean> mSceneList) {
        mDBind.sceneWithoutAuto.setVisibility(View.GONE);
        mDBind.sceneHasAuto.setVisibility(View.VISIBLE);
        mAdapter.setList(mSceneList);
    }

    @Override
    public void withoutAuto() {
        mAdapter.setList(null);
        mDBind.sceneHasAuto.setVisibility(View.GONE);
        mDBind.sceneWithoutAuto.setVisibility(View.VISIBLE);
    }

    @Override
    public void skipActivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void finishActivity() {
        finish();
    }
}
